package controllers

import (
	"crypto/md5"
	"encoding/hex"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"

	// "github.com/bitly/go-simplejson"
	"encoding/json"
	"github.com/3xxx/engineercms/models"
	"net/http"
	"strconv"
	"time"
)

// type Userselect struct { //
// 	Id   int64  `json:"id"`
// 	Name string `json:"text"`
// }

type RegistController struct {
	beego.Controller
}

func (this *RegistController) Get() {
	this.TplName = "regist.tpl"
}

func (this *RegistController) RegistErr() {
	this.TplName = "registerr.tpl"
}

func (this *RegistController) CheckUname() {
	var user models.User //这里修改
	inputs := this.Input()
	//fmt.Println(inputs)
	user.Username = inputs.Get("uname")
	err := models.CheckUname(user) //这里修改
	if err == nil {
		this.Ctx.WriteString("false")
		// return false
	} else {
		this.Ctx.WriteString("true")
		// return true
	}
	// return
}

//提交注册名称
func (this *RegistController) Post() {
	var user models.User //这里修改
	inputs := this.Input()
	logs.Info(inputs)
	user.Username = inputs.Get("uname")
	user.Email = inputs.Get("email")
	user.Nickname = inputs.Get("nickname")
	Pwd1 := inputs.Get("pwd")

	md5Ctx := md5.New()
	md5Ctx.Write([]byte(Pwd1))
	cipherStr := md5Ctx.Sum(nil)
	// fmt.Print(cipherStr)
	// fmt.Print("\n")
	// fmt.Print(hex.EncodeToString(cipherStr))
	user.Password = hex.EncodeToString(cipherStr)
	user.LastLoginTime = time.Now()
	user.Status = 1
	user.Role = "4"
	_, err := models.SaveUser(user) //这里修改

	// _, err = models.AddRoleUser(4, uid)
	if err == nil {
		this.TplName = "success.tpl"
	} else {
		// fmt.Println(err)
		this.TplName = "registerr.tpl"
	}
}

// @Title post wx regist
// @Description post wx regist
// @Param uname query string  true "The username of ueser"
// @Param password query string  true "The password of account"
// @Param code query string  true "The code of wx"
// @Param app_version query string  true "The app_version of wx"
// @Success 200 {object} models.SaveUser
// @Failure 400 Invalid page supplied
// @Failure 404 user not found
// @router /wxregist [post]
//添加微信小程序珠三角设代阅览版注册
func (c *RegistController) WxRegist() {
	var user models.User
	var uid string
	var isAdmin bool
	user.Username = c.Input().Get("uname")
	Pwd1 := c.Input().Get("password") //注意这里用的是全称password，不是pwd
	// autoLogin := c.Input().Get("autoLogin") == "on"
	md5Ctx := md5.New()
	md5Ctx.Write([]byte(Pwd1))
	cipherStr := md5Ctx.Sum(nil)
	user.Password = hex.EncodeToString(cipherStr)
	// logs.Info(user.Password)
	// logs.Info(user.Username)
	err := models.ValidateUser(user)
	if err == nil {
		JSCODE := c.Input().Get("code")
		logs.Info(JSCODE)
		var APPID, SECRET string
		app_version := c.Input().Get("app_version")
		if app_version == "1" {
			APPID = beego.AppConfig.String("wxAPPID")
			SECRET = beego.AppConfig.String("wxSECRET")
		} else if app_version == "4" {
			APPID = beego.AppConfig.String("wxAPPID4")
			SECRET = beego.AppConfig.String("wxSECRET4")
			// logs.Info(APPID)
		}
		requestUrl := "https://api.weixin.qq.com/sns/jscode2session?appid=" + APPID + "&secret=" + SECRET + "&js_code=" + JSCODE + "&grant_type=authorization_code"
		resp, err := http.Get(requestUrl)
		if err != nil {
			logs.Error(err)
			// return
		}
		defer func() {
			_ = resp.Body.Close()
		}()
		if resp.StatusCode != 200 {
			logs.Error(err)
		}
		var data map[string]interface{}
		err = json.NewDecoder(resp.Body).Decode(&data)
		if err != nil {
			logs.Error(err)
		}
		// logs.Info(data)
		var openID string
		// var sessionKey string
		if _, ok := data["session_key"]; !ok {
			errcode := data["errcode"]
			// logs.Info(errcode)
			errmsg := data["errmsg"].(string)
			c.Data["json"] = map[string]interface{}{"errNo": errcode, "msg": errmsg, "data": "session_key 不存在"}
		} else {
			openID = data["openid"].(string)
			// logs.Info(openID)
			//将openid写入数据库
			user, err = models.GetUserByUsername(user.Username)
			if err != nil {
				logs.Error(err)
			} else {
				uid = strconv.FormatInt(user.Id, 10)
			}
			_, err = models.AddUserOpenID(user.Id, openID)
			if err != nil {
				logs.Error(err)
			}
			// roles, err := models.GetRolenameByUserId(user.Id)
			// if err != nil {
			// 	logs.Error(err)
			// }
			// var isAdmin bool
			// for _, v := range roles {
			// 	if v.Rolename == "admin" {
			// 		isAdmin = true
			// 	}
			// }
			role, err := models.GetRoleByRolename("admin")
			if err != nil {
				logs.Error(err)
			} else {
				roleid := strconv.FormatInt(role.Id, 10)
				isAdmin = e.HasRoleForUser(uid, "role_"+roleid)
			}
			// logs.Info(isAdmin)
			// logs.Info(user.Id)
			if err != nil {
				logs.Error(err)
				c.Data["json"] = map[string]interface{}{"info": "已经注册", "data": ""}
				c.ServeJSON()
			} else {
				c.Data["json"] = map[string]interface{}{"info": "SUCCESS", "userId": uid, "isAdmin": isAdmin}
				c.ServeJSON()
			}
		}
	} else {
		c.Data["json"] = map[string]interface{}{"info": "用户名或密码错误", "data": ""}
		c.ServeJSON()
	}
}

//post方法
func (this *RegistController) GetUname() {
	var user models.User //这里修改[]*models.User(uname string)
	inputs := this.Input()
	//fmt.Println(inputs)
	user.Username = inputs.Get("uname")
	// logs.Info(user.Username)
	uname1, err := models.GetUname(user) //这里修改
	//转换成json数据？
	// logs.Info(uname1[0].Username)
	// b, err := json.Marshal(uname1)
	if err == nil {
		// this.Ctx.WriteString(string(b))
		this.Data["json"] = uname1 //string(b)
		this.ServeJSON()
	}
	// 	this.Ctx.WriteString(uname1[1].Username)
	// 	// return uname1[0].Username
	// }
	// return uname1[0].Username
}

//get方法，用于x-editable的select2方法
func (this *RegistController) GetUname1() {
	var user models.User //这里修改[]*models.User(uname string)
	inputs := this.Input()
	//fmt.Println(inputs)
	user.Username = inputs.Get("uname")
	// logs.Info(user.Username)
	uname1, err := models.GetUname(user) //这里修改
	//转换成json数据？
	// logs.Info(uname1[0].Username)
	// b, err := json.Marshal(uname1)
	if err != nil {
		logs.Error(err)
	}
	slice1 := make([]Userselect, 0)
	for _, v := range uname1 {
		aa := make([]Userselect, 1)
		aa[0].Id = v.Id //这里用for i1,v1,然后用v1.Id一样的意思
		// aa[0].Ad = v.Id
		aa[0].Name = v.Username
		slice1 = append(slice1, aa...)
	}
	if err != nil {
		logs.Error(err)
	}
	this.Data["json"] = slice1 //string(b)
	this.ServeJSON()
}
